package com.wangzeqi.changliaoba.messege;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wangzeqi.changliaba.dao.SingleDataDao;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseFragment;
import com.wangzeqi.changliaoba.singleton.AllSingleTon;

import java.util.ArrayList;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/2/29.
 */
public class MessegeFragment extends BaseFragment implements View.OnClickListener {
    private EditText editTextQuery;
    private ImageView imageViewQuxiao;
    private ListView listViewMesseges;
    private MessegeListViewAdapter adapter;
    private ArrayList<SingleData> datas;
    private String[] data = new String[3];

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_messege, null);
        listViewMesseges = (ListView) view.findViewById(R.id.messege_lv);
        QueryBuilder qb = AllSingleTon.getOurInstance().getDaoSession().getSingleDataDao().queryBuilder();
//        qb.where(SingleDataDao.Properties.)
        datas = new ArrayList<>();
        adapter = new MessegeListViewAdapter(getActivity(), datas);
        listViewMesseges.setAdapter(adapter);
        editTextQuery = (EditText) view.findViewById(R.id.messege_query_et);
        imageViewQuxiao = (ImageView) view.findViewById(R.id.messege_quxiao_iv);
        editTextQuery.addTextChangedListener(new MessegeQueryChangeListener(getActivity(), imageViewQuxiao, editTextQuery));
        imageViewQuxiao.setOnClickListener(this);
        ChatActivity.handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 100) {
                    data = (String[]) msg.obj;
                }
                SingleData singleData = new SingleData(data[0], data[1], data[2]);
                if (datas.size() > 0) {
                    for (int i = 0; i < datas.size(); i++) {
                        if (datas.get(i).getName().equals(data[0])) {
                            datas.remove(i);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
                datas.add(0, singleData);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.messege_quxiao_iv:
                editTextQuery.setText("");
                imageViewQuxiao.setVisibility(View.INVISIBLE);
                break;


        }
    }
}
